/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package deti.es.ukni.rest;

import deti.es.persistence.account.AccountDoesNotExist;
import deti.es.persistence.calendar.CalendarManagerRemote;
import deti.es.ukni.cm.persistence.DAOFactory;
import deti.es.ukni.persistence.entity.Event;
import java.util.Set;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.PathParam;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

/**
 * REST Web Service
 *
 * @author ukni
 */

public class BandEventResource {
    @Context
    private UriInfo context;

    /** Creates a new instance of BandResource */
    public BandEventResource() {
    }

    /**
     * Retrieves representation of an instance of deti.es.ukni.rest.BandResource
     * @param name resource URI parameter
     * @return an instance of java.lang.String
     */
    @GET
    @Produces("text/plain")
    public String getText( @PathParam("id") Integer IDBand) {

        JSONArray events = new JSONArray();

        Set<Event> listevent;
        CalendarManagerRemote CMBean;
        CMBean = DAOFactory.getCalendarManager();
        try {
             listevent = CMBean.findBandEvents(IDBand);
        } catch (AccountDoesNotExist ex) {
            return events.toJSONString();
        }

        for( Event e: listevent ){
            JSONObject event = new JSONObject();

            event.put("id", e.getIdevent());
            event.put("location", e.getLocation());
            event.put("eventdate", e.getEventdate().toString());
            event.put("addtime", e.getDatecreation().getTime());
            event.put("lat", e.getLatitude());
            event.put("lng", e.getLongitude());

            events.add(event);
        }

        return events.toJSONString();
    }
}
